package com.lm.liaoming.tool;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.Log;

import com.lm.liaoming.bean.VersionInfoBean;
import com.lm.liaoming.constant.ConstantValues;
import com.tumblr.remember.Remember;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * created by 韦敏敏
 * on 2019/11/18
 */
public class Utils {
    private static final String TAG = "lm";
    public static void i(String msg){
        Log.i(TAG, msg);
    }
    /*获取当前应用的版本号*/
    public static VersionInfoBean getVersionName(Context context){
        VersionInfoBean versionInfoBean = new VersionInfoBean();
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            /*版本号*/
            int versionCode = packageInfo.versionCode;
            /*版本名称*/
            String versionName = packageInfo.versionName;
            versionInfoBean.setVersionCode(versionCode);
            versionInfoBean.setVersionName(versionName);
            Remember.putString(ConstantValues.VERSION_NAME, versionName);
            Remember.putInt(ConstantValues.VERSION_CODE, versionCode);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionInfoBean;
    }
    // 创建一个线程数固定大小为10的线程池
    public static ExecutorService executorService = Executors.newFixedThreadPool(10);
    // 获取当前进程名

    /**
     * 获取进程号对应的进程名
     * @param pid
     * @return
     */
    public static String getProcessName(int pid){
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if(!TextUtils.isEmpty(processName)){
                processName = processName.trim();
            }
            return processName;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
